<template>
  <div class="add-edit-role-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="$t('tables_title.name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.superior_role')" prop="pName">
        <div class="deptname-input" @click="innerVisible = true"></div>
        <el-input v-model="ruleForm.pName" :placeholder="$t('tables_title.superior_role')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.alias')" prop="description">
        <el-input v-model="ruleForm.description" :placeholder="$t('tables_title.alias')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input type="number" v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>

    <el-dialog width="350px" :visible.sync="innerVisible" append-to-body>
        <div class="role-tree-view">
          <div class="user-tree-data-view">
            <el-tree :data="roleTreeData" :props="treeProps" node-key="id" default-expand-all :expand-on-click-node="false"
            @node-click="handleNodeClick"></el-tree>
          </div>
          <div style="text-align: center;">
            <el-button @click="innerVisible = false">{{$t('global.cancel')}}</el-button>
          </div>
        </div>
    </el-dialog>
  </div>
</template>

<script>

import { getRoleTreeList,addRole,editRole } from '@/api/roleApi.js'

export default {
  data(){
    return{
      loading:false,
      innerVisible:false,
      roleTreeData:[],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      ruleForm:{
        name:'',
        pName:'',
        pid:'',
        description:'',
        sort:''
      },
      rules:{
          name: [
            { required: true, message: this.$t('role_management.empty_role_name'), trigger: 'blur' },
          ],
          pName: [
            { required: true, message: this.$t('role_management.empty_super_role'), trigger: 'blur' },
          ],
          description: [
            { required: true, message: this.$t('role_management.empty_alias'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ["isEdit",'editRoleInfo'],
  watch:{
    editRoleInfo(newVal,oldVal){
      this.ruleForm = newVal
    }
  },
  components:{

  },
  created(){
    this.getRoleList()
  },
  methods:{
    getRoleList(){              //获取角色列表
      getRoleTreeList().then((res)=>{
        if(res.status == 200){
          this.roleList = res.data
          this.roleTreeData = this.getMenuList(0)
        }
      }).catch((e)=>{

      })
    },
    getMenuList(pId){           //将角色列表分类成树形结构
      const temp = [];
      for (const item of this.roleList) {
        if (item.pId === pId && item.id != 0) {
          item.children = this.getMenuList(item.id);
          temp.push(item);
        }
      }
      return temp;
    },
    handleNodeClick(data){      //树形节点点击触发事件
      console.log(data)
      this.ruleForm.pid = data.id
      this.ruleForm.pName = data.name
      this.innerVisible = false
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = new FormData()
          formData.append("name",this.ruleForm.name)
          formData.append("pName",this.ruleForm.pName)
          formData.append("pid",this.ruleForm.pid)
          formData.append("description",this.ruleForm.description)
          formData.append("sort",this.ruleForm.sort?this.ruleForm.sort:1)
          if(this.ruleForm.roleId){
            formData.append("roleId",this.ruleForm.roleId)
            this.userEditRole(formData)
          }else{
            this.userAddRole(formData)
          }
        } else {
          return false;
        }
      });
    },
    userEditRole(formData){         //修改role信息
      this.loading = true
      editRole(formData).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.getRoleList()        //重新获取role信息
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    userAddRole(formData){          //新增管Role
      this.loading = true
      addRole(formData).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.getRoleList()          //重新获取role信息
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>

.add-edit-role-view{
  padding-right: 30px;
  padding-left: 30px;
}

.role-tree-view{
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: column;
  .user-tree-data-view{
    flex: 1;
    width: 100%;
    overflow: auto;
    margin-bottom: 20px;
  }
}
</style>
